//
//  Status.swift
//  Weibo
//
//  Created by 孙成宇 on 15/11/30.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class Status: NSObject {
   //微博创建时间
    var created_at: String?
  //用户ID
    var id: Int = 0
    //
    var text: String?
    
    var source: String?
    
    var user: User?

    var retweeted_status: Status?

    
    
    //配图数组属性
    var pic_urls: [[String : String]]? {
        didSet{
        //给imageURLs赋值
            guard let urls = pic_urls else {
            
            //终止set造作
            return
            
            }
            
            imageURLs = [NSURL]()
        
            
            
            //遍历urls
            for item in urls {
                
                let urlString = item["thumbnail_pic"]

    urlString?.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")

               //相信后端的urlString一定能转化成url
                let url = NSURL(string: urlString!)!
                
                imageURLs?.append(url)
                
        }
    
        }
    
    }
    //初始化imageURLs
    var imageURLs: [NSURL]?

    var pictureURLs: [NSURL]? {



        if retweeted_status != nil{
     return retweeted_status?.imageURLs

        }
        return imageURLs

    }


    
    //构造方法  KVC 设置初始值
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)

        
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user"{
            if let dict = value as? [String : AnyObject] {
            
            user = User(dict: dict)
                
            
            }
        
        return
        }
        if key == "retweeted_status"{
            if let dict = value as? [String : AnyObject] {

               retweeted_status = Status(dict: dict)


            }

            return
        }

        super.setValue(value, forKey: key)
        
    }
    
    
    
    
    
    
    
    
    
    //过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    
    
    //重写description 属性
    override var description: String {
        let keys = ["created_at","id","text","source"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
